package youdu

import (
	"encoding/json"
	"fmt"
	"youdu/model"
)

const (
	API_PATH_GROUP_CREATE    string = "/cgi/group/create"
	API_PATH_GROUP_DELETE    string = "/cgi/group/delete"
	API_PATH_GROUP_UPDATE    string = "/cgi/group/update"
	API_PATH_GROUP_INFO      string = "/cgi/group/info"
	API_PATH_GROUP_LIST      string = "/cgi/group/list"
	API_PATH_GROUP_ADDMEMBER string = "/cgi/group/addmember"
	API_PATH_GROUP_DELMEMBER string = "/cgi/group/delmember"
	API_PATH_GROUP_ISMEMBER  string = "/cgi/group/ismember"
)

// 创建群
func (c *Org) GroupCreate(name string) (string, error) {
	api := API_PATH_GROUP_CREATE

	// 检查群名称长度
	if len(name) > 32 {
		return "", fmt.Errorf("群名称不能超过32个字符")
	}

	data := struct {
		Name string `json:"name"`
	}{
		Name: name,
	}

	resp, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, data)
	if err != nil {
		return "", err
	}

	var result struct {
		Id string `json:"id"`
	}
	if err := json.Unmarshal(resp, &result); err != nil {
		return "", err
	}

	return result.Id, nil
}

// 删除群
func (c *Org) GroupDelete(id string) error {
	api := fmt.Sprintf("%s?id=%s", API_PATH_GROUP_DELETE, id)
	_, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, nil)
	return err
}

// 修改群名称
func (c *Org) GroupUpdate(id string, name string) error {
	api := API_PATH_GROUP_UPDATE

	// 检查群名称长度
	if len(name) > 32 {
		return fmt.Errorf("群名称不能超过32个字符")
	}

	data := struct {
		Id   string `json:"id"`
		Name string `json:"name"`
	}{
		Id:   id,
		Name: name,
	}

	_, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, data)
	return err
}

// 查看群信息
func (c *Org) GroupInfoGet(id string) (*model.GroupInfo, error) {
	api := fmt.Sprintf("%s?id=%s", API_PATH_GROUP_INFO, id)
	var groupInfo model.GroupInfo

	resp, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, nil)
	if err != nil {
		return nil, err
	}

	if err := json.Unmarshal(resp, &groupInfo); err != nil {
		return nil, err
	}

	return &groupInfo, nil
}

// 获取群列表
func (c *Org) GroupListGet(userId string) (*model.GroupList, error) {
	api := API_PATH_GROUP_LIST
	if userId != "" {
		api = fmt.Sprintf("%s?userId=%s", api, userId)
	}

	var groupList model.GroupList
	resp, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, nil)
	if err != nil {
		return nil, err
	}

	if err := json.Unmarshal(resp, &groupList); err != nil {
		return nil, err
	}

	return &groupList, nil
}

// 添加群成员
func (c *Org) GroupAddMember(id string, userList []string) error {
	api := API_PATH_GROUP_ADDMEMBER

	data := struct {
		Id       string   `json:"id"`
		UserList []string `json:"userList"`
	}{
		Id:       id,
		UserList: userList,
	}

	_, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, data)
	return err
}

// 删除群成员
func (c *Org) GroupDelMember(id string, userList []string) error {
	api := API_PATH_GROUP_DELMEMBER

	data := struct {
		Id       string   `json:"id"`
		UserList []string `json:"userList"`
	}{
		Id:       id,
		UserList: userList,
	}

	_, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, data)
	return err
}

// 查询用户是否为群成员
func (c *Org) GroupIsMember(id string, userId string) (bool, error) {
	api := fmt.Sprintf("%s?id=%s&userId=%s", API_PATH_GROUP_ISMEMBER, id, userId)

	resp, err := Tools.ApiRequest(c.Server, c.Buin, c.AppId, c.EncodedAesKey, api, nil)
	if err != nil {
		return false, err
	}

	var result struct {
		Belong bool `json:"belong"`
	}
	if err := json.Unmarshal(resp, &result); err != nil {
		return false, err
	}

	return result.Belong, nil
}
